package org.example.medicalstaff.controller;

import com.alibaba.fastjson.JSONObject;
import net.sf.jsqlparser.expression.DateTimeLiteralExpression;
import org.example.his.common.api.CommonResponse;
import org.example.his.common.utils.RequestToJsonObject;
import org.example.medicalstaff.entity.Prescripition;
import org.example.medicalstaff.entity.PrescriptionDetailed;
import org.example.medicalstaff.service.PrescriptionService;
import org.example.medicalstaff.vo.DrugAndPreDetailed;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.math.BigDecimal;
import java.util.List;

@RestController
@RequestMapping("/out_doctor")
public class PrescriptionController {

    @Autowired
    private PrescriptionService prescriptionService;
    /**
     * CH AREA
     */

    /**
     * API1 暂存开立处方
     * url格式：http://localhost:9090/out_doctor/prescription/tem
     * 备注：状态我设置为了1，暂存
     * 测试结果：正常
     */
    @PostMapping("/prescription/tem")
    public CommonResponse<Prescripition> tem_store_pres(
            HttpServletRequest httpRequest
    ){
        JSONObject jsonObject = RequestToJsonObject.RequestToJsonObject(httpRequest);
        int MedicalID = Integer.parseInt((String) jsonObject.get("MedicalID"));
        int RegistID = (int) jsonObject.get("RegistID");
        int UserID = (int) jsonObject.get("UserID");
        String PrescriptionName = (String)jsonObject.get("PrescriptionName");
        //要把时间转换为Date再存入
        return prescriptionService.tem_store_pres(
            MedicalID,
            RegistID,
            UserID,
            PrescriptionName
        );
    }

    /**
     * API2 根据药品处方获取药品明细VO
     * url格式如下：http://localhost:9090/out_doctor/prescription/getDetailByPre?MedicalID=20&RegistID=37&PrescriptionName=模板：支气管哮喘&UserID=1
     * 备注：这个不能用请求体的方式，要用 ？A=B 的形式来传参
     * 测试结果：正常
     */
    @PostMapping("/prescription/getDetailByPre")
    public CommonResponse<List<DrugAndPreDetailed>> getDetailByPre(
            HttpServletRequest httpRequest
    ){
        JSONObject jsonObject = RequestToJsonObject.RequestToJsonObject(httpRequest);
        int MedicalID = Integer.parseInt((String) jsonObject.get("MedicalID"));
        int RegistID = (int) jsonObject.get("RegistID");
        int UserID = (int) jsonObject.get("UserID");
        String PrescriptionName = (String)jsonObject.get("PrescriptionName");
        return prescriptionService.getDetailByPre(
            MedicalID,
            RegistID,
            UserID,
            PrescriptionName
        );
    }

    /**
     * API3 修改暂存开立处方
     * url格式如下：http://localhost:9090/out_doctor/prescription/modify_tem
     * 备注：只有名字和状态可以修改，时间也是不能修改的。
     * 测试结果：正常
     */
    @PostMapping("/prescription/modify_tem")
    public CommonResponse<String> modify_tem(
            HttpServletRequest httpRequest
    ){
        JSONObject jsonObject = RequestToJsonObject.RequestToJsonObject(httpRequest);
        int MedicalID = Integer.parseInt((String) jsonObject.get("MedicalID"));
        int RegistID = (int) jsonObject.get("RegistID");
        int UserID = (int) jsonObject.get("UserID");
        String PrescriptionName = (String)jsonObject.get("PrescriptionName");
        return prescriptionService.modify_tem(
            MedicalID,
            RegistID,
            UserID,
            PrescriptionName
        );
    }

    /**
     * API4 删除暂存开立处方
     * url格式如下：http://localhost:9090/out_doctor/prescription/del_tem
     * 备注：无
     * 测试结果：正常
     */
    @PostMapping("/prescription/del_tem")
    public CommonResponse<String> del_tem(
            HttpServletRequest httpRequest
    ){
        JSONObject jsonObject = RequestToJsonObject.RequestToJsonObject(httpRequest);
        int MedicalID = Integer.parseInt((String) jsonObject.get("MedicalID"));
        int RegistID = (int) jsonObject.get("RegistID");
        int UserID = (int) jsonObject.get("UserID");
        String PrescriptionName = (String)jsonObject.get("PrescriptionName");
        return prescriptionService.del_tem(
                MedicalID,
                RegistID,
                UserID,
                PrescriptionName
        );
    }

    /**
     * API5 暂存处方增加药品
     * url格式如下：http://localhost:9090/out_doctor/prescription/add
     * 备注：这个同样没有设置主外键约束，所以前端传值过来的时候注意别写错了，不然就会出现不合理的值
     * 测试结果：正常
     */
    @PostMapping("/prescription/add")
    public CommonResponse<String> addItem(
            HttpServletRequest httpRequest
    ){
        //规格，单价
        JSONObject jsonObject = RequestToJsonObject.RequestToJsonObject(httpRequest);
        int PrescriptionID = (int) jsonObject.get("PrescriptionID");
        int DrugsID = (int) jsonObject.get("DrugsID");
        String DrugsUsage = (String)jsonObject.get("DrugsUsage");
        String Dosage = (String) jsonObject.get("Dosage");
        String Frequency = (String)jsonObject.get("Frequency");
        int Amount = Integer.parseInt((String)(jsonObject.get("Amount")));
        return prescriptionService.addItem(
            PrescriptionID,
            DrugsID,
            DrugsUsage,
            Dosage,
            Frequency,
            Amount
        );
    }

    /**
     * API6 暂存处方删除药品
     * url格式如下：http://localhost:9090/out_doctor/prescription/del_m
     * 备注：无
     * 测试结果：正常
     */
    @PostMapping("/prescription/del_m")
    public CommonResponse<String> del_m(
            HttpServletRequest httpRequest
    ){
        JSONObject jsonObject = RequestToJsonObject.RequestToJsonObject(httpRequest);
        int PrescriptionID = (int) jsonObject.get("PrescriptionID");
        String DrugsName = (String) jsonObject.get("DrugsName");
        String DrugsFormat = (String) jsonObject.get("DrugsFormat");
        BigDecimal DrugsPrice = (BigDecimal) jsonObject.get("DrugsPrice");
        return prescriptionService.del_m(PrescriptionID,DrugsName,DrugsFormat,DrugsPrice);
    }

    /**
     * API7 获取当前状态的所有处方
     */
    @PostMapping("/prescription/get_pre")
    public CommonResponse<List<Prescripition>> get_pre(
            HttpServletRequest httpRequest

    ){
        JSONObject jsonObject = RequestToJsonObject.RequestToJsonObject(httpRequest);
        System.out.println(jsonObject+ " 174");
        int MedicalID = Integer.parseInt((String) jsonObject.get("MedicalID"));
        int RegistID = (int) jsonObject.get("RegistID");
        int UserID = (int) jsonObject.get("UserID");
        return prescriptionService.get_pre(MedicalID,RegistID,UserID);
    }



    /**
     * ZCJ AREA
     */

    /**
     * 新增处方使用组套药品
     * 测试URL：http://127.0.0.1:9090/out_doctor/prescription/use_set
     * 备注：参数中ID和Name是必须在表drugstemplate中有的，就是得先匹配这个表才能在prescription表中新增,需要多一个参数Amount（Amount是随意值）
     */
    @PostMapping("/prescription/use_set")
    public CommonResponse<Prescripition> use_set(
            HttpServletRequest httpRequest
    ){
        JSONObject jsonObject = RequestToJsonObject.RequestToJsonObject(httpRequest);
        System.out.println(jsonObject+ " 174");
        int id = (int) jsonObject.get("id");
        int MedicalID = Integer.parseInt((String) jsonObject.get("MedicalID"));
        int RegistID = (int) jsonObject.get("RegistID");
        int UserID = (int) jsonObject.get("UserID");
        BigDecimal Amount = new BigDecimal((int) jsonObject.get("Amount"));
        return prescriptionService.prescripiton_managePrescripition_add(id,MedicalID,RegistID,UserID,Amount);
    }
    /**
     * 修改处方使
     * 测试URL：http://127.0.0.1:9090/out_doctor/prescription/use_set
     * 备注：这个方法仅修改了Prescription中的State,使之变为2，也就是说让它成为已开立
     */
    @PutMapping("/prescription/init")
    public CommonResponse<Prescripition> update_state(
            @RequestParam("MedicalID")int MedicalID,
            @RequestParam("RegistID")int RegistID,
            @RequestParam("UserID")int UserID,
            @RequestParam("PrescriptionName") String PrescriptionName
    ){
        return prescriptionService.prescripiton_managePrescripition_update(MedicalID,RegistID,UserID,PrescriptionName);
    }

}
